user.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * 用户模型
  3. */
  4. define(['base', '$', 'native', 'api', 'order', 'config', 'address'], function(base, $, native, api, order, config, address) {
  5. var User = function() {
  6. if (typeof User.instance === 'object') {
  7. return User.instance;
  8. }
  9. User.instance = this;
  10. this.storagePrefix = 'user_';
  11. this.id = '';
  12. this.lastID = this.getCache('lastID', null, '');
  13. this.name = '';
  14. this.mobile = '';
  15. this.balance = 0;
  16. this.info = {};
  17. this.orders = {
  18. 1: {
  19. data: [],
  20. page: 0,
  21. hasMore: false
  22. },
  23. 2: {
  24. data: [],
  25. page: 0,
  26. hasMore: false
  27. },
  28. 3: {
  29. data: [],
  30. page: 0,
  31. hasMore: false
  32. },
  33. 4: {
  34. data: [],
  35. page: 0,
  36. hasMore: false
  37. },
  38. 5: {
  39. data: [],
  40. page: 0,
  41. hasMore: false
  42. }
  43. };
  44. this.appendOrder = {};//追加订单
  45. this.orderCoupons = [];// 所有优惠券按顺序排好
  46. this.usable_coupons = [];// 可用优惠券
  47. this.used_coupons = [];// 用过的优惠券
  48. this.overtime_coupons = [];// 过期的优惠券
  49. this.activities = this.getCache('activities', null, {//活动
  50. "visited_dog": false,
  51. "visited_order0104": false
  52. });
  53. this.productVisit = this.getCache('productVisit', null, {//产品访问-缓存为空
  54. 1: {
  55. visited: false//访问
  56. },
  57. 2: {
  58. visited: false
  59. },
  60. 3: {
  61. visited: false
  62. },
  63. 4: {
  64. visited: false
  65. },
  66. 5: {
  67. visited: false
  68. },
  69. 6: {
  70. visited: false
  71. },
  72. 7: {
  73. visited: false
  74. },
  75. 8: {
  76. visited: false
  77. },
  78. 9: {
  79. visited: false
  80. },
  81. 10: {
  82. visited: false
  83. },
  84. 11: {
  85. visited: false
  86. },
  87. 12: {
  88. visited: false
  89. },
  90. 13: {
  91. visited: false
  92. },
  93. 14: {
  94. visited: false
  95. },
  96. 15: {
  97. visited: false
  98. }
  99. });
  100. this.selectTech = {//选择...技术?
  101. can: [],//能够
  102. match: []//匹配
  103. };
  104. this.isCheck = this.getCache('isCheck', null, {//检测缓存中是否有优惠券数据
  105. "couponCheck": false//优惠券是否存在
  106. });
  107. }
  108. User.prototype = new base();//新建base()函数.继承User函数的原型
  109. //获取用户信息
  110. User.prototype.getUserInfo = function(callback){
  111. var that = this;
  112. native.getUserInfo(function(res) {
  113. loginWithData.call(that, res);//loginWithData-登录数据
  114. if (typeof(callback) == 'function') {//判断callback是否为函数.
  115. callback();//调用函数
  116. }
  117. });
  118. };
  119. //获取优惠券名字
  120. User.prototype.getCouponName = function (couponID) {
  121. var couponName = '';//优惠券名字为空
  122. for (var i in this.orderCoupons) {//将代购的优惠券信息赋给 i
  123. var couponInfo = this.orderCoupons[i];//优惠券信息为 this.orderCoupons[i];
  124. if (couponInfo.id == couponID) {//判断优惠券信息是否真实
  125. couponName = couponInfo['coupon']['name'];//若为真返回优惠券的名字
  126. break;
  127. }
  128. }
  129. return couponName;
  130. };
  131. //获取优惠券价格
  132. User.prototype.getCouponValue = function (couponID) {
  133. var couponValue = 0;
  134. for (var i in this.orderCoupons) {
  135. var couponInfo = this.orderCoupons[i];
  136. if (couponInfo.id == couponID) {//判断优惠券是否存在
  137. couponValue = couponInfo['coupon']['value'];//value为优惠券价格
  138. break;
  139. }
  140. }
  141. return couponValue;
  142. };
  143. //获取各类产品的优惠券
  144. User.prototype.getProductCoupon = function(products, time, type, callback){
  145. var that = this;
  146. api.getCouponList({
  147. products: products,//产品
  148. booking_time: time,//预订时间
  149. user_id: this.id,//用户id
  150. type: type//资源
  151. }, function(res) {
  152. if (res.success) {
  153. that.orderCoupons = res.data;//订购优惠券
  154. that.unuseable_coupons = res.unuseable_coupons;//不可用的优惠券
  155. }
  156. if (typeof(callback) == 'function') {//true 回调函数 中将传值res
  157. callback(res);
  158. }
  159. });
  160. };
  161. //优惠券列表
  162. User.prototype.getCouponList = function(userId, callback){
  163. var that = this;
  164. api.getCouponList({
  165. get_all: 1,
  166. user_id: userId // 这...
  167. }, function(res) {
  168. if (res.success) {
  169. // 现有可用优惠券,按照面额降序排序;已经过期的优惠券,按照过期时间由近到远排序
  170. var now = new Date().getTime() / 1000;
  171. that.usable_coupons = res.data.useable_coupons;
  172. that.used_coupons = res.data.used_coupons;
  173. that.overtime_coupons = res.data.overtime_coupons;
  174. that.usable_coupons.sort(function (a, b) {
  175. return b.coupon.value - a.coupon.value;
  176. });
  177. that.used_coupons.sort(function (a, b) {
  178. return a.end_time - b.end_time;
  179. });
  180. that.overtime_coupons.sort(function (a, b) {
  181. return a.end_time - b.end_time;
  182. });
  183. that.usable_coupons.forEach(function(item, index){
  184. var isRemind = (item.end_time - now) / (60 * 60 * 24) <= 7;
  185. item.isRemind = isRemind;
  186. });
  187. that.orderCoupons = that.usable_coupons.concat(that.used_coupons).concat(that.overtime_coupons);
  188. }
  189. if (typeof(callback) == 'function') {
  190. callback(res);
  191. }
  192. });
  193. };
  194. //兑换优惠券 api预先定义的函数
  195. User.prototype.exchangeCoupon = function(couponCode, callback){
  196. var that = this;
  197. api.exchangeCoupon({
  198. user_id: that.id,
  199. exchange_code: couponCode//兑换码--为优惠券编码
  200. }, function(res) {
  201. if (typeof(callback) == 'function') {
  202. callback(res);//回调函数传回
  203. }
  204. });
  205. };
  206. //判断当前用户是否登录.没有则让其去登录
  207. User.prototype.goLogin = function(callback){
  208. var that = this;
  209. this.getUserInfo(function(res) {//在获取用户信息
  210. if (that.id == '') {
  211. native.login(function(resA) {//本地中读取是否存在登陆数据
  212. loginWithData.call(that, resA);//调用登录数据
  213. if (typeof(callback) == 'function') {//判断callback是否为函数,true让其回调
  214. callback();
  215. }
  216. });
  217. } else {
  218. if (typeof(callback) == 'function') {
  219. callback();
  220. }
  221. }
  222. });
  223. };
  224. //是否检查登陆凭据是否存在
  225. User.prototype.checkLogin = function(callback){
  226. var that = this;
  227. if (this.id == '') {
  228. this.goLogin(function() {
  229. if (that.id != '' && typeof(callback) == 'function') {//用户ID是否为空并且callback是否为函数
  230. callback();
  231. }
  232. });
  233. } else {
  234. if (typeof(callback) == 'function') {
  235. callback();
  236. }
  237. }
  238. };
  239. //获取订单列表
  240. User.prototype.getOrderList = function(userId, type, callback, more){
  241. var that = this;
  242. var page = this.orders[type].page + 1;
  243. if (!more) {
  244. page = 1;
  245. }
  246. api.getMyOrderList({
  247. user_id: userId,
  248. type: type,
  249. page: page
  250. }, function(res) {
  251. if (res.current_page >= res.sum_page) {//如果当前页订单大于等于总页数
  252. res.current_page = res.sum_page;
  253. that.orders[type].hasMore = false;
  254. } else {
  255. that.orders[type].hasMore = true;
  256. }
  257. that.orders[type].page = res.current_page;//当前网页
  258. if (more) {
  259. that.orders[type].data = that.orders[type].data.concat(res.data);//concat 2个数组
  260. } else {
  261. that.orders[type].data = res.data;
  262. }
  263. if (typeof(callback) == 'function') {
  264. callback(res);
  265. }
  266. });
  267. };
  268. //订单明细
  269. User.prototype.getOrderDetail = function(orderID, callback) {
  270. var that = this;
  271. api.getOrderDetail({
  272. user_id: that.id,
  273. order_id: orderID//订单ID为orderID
  274. }, function(res) {
  275. if (typeof(callback) == 'function') {
  276. callback(res);
  277. }
  278. });
  279. };
  280. //订单信息
  281. //遍历函数。将所有产生的订单信息赋值给orderInfo
  282. User.prototype.getOrderInfo = function(orderID){
  283. var that = this;
  284. var orderInfo = {};//数组
  285. var orderList = this.orders[1].data;
  286. orderList.forEach(function(e, i) {//遍历函数
  287. if (e.id == orderID ) {
  288. orderInfo = e;
  289. }
  290. })
  291. return orderInfo;
  292. };
  293. //退款单
  294. User.prototype.refundOrder = function(orderID, callback) {
  295. var that = this;
  296. var from = '';
  297. if (config.isChubao) from = 'chubao';
  298. api.refundOrder({
  299. order_id: orderID,
  300. user_id: this.id,
  301. from: from
  302. }, function(res){
  303. if (typeof(callback) == 'function') {
  304. callback(res);
  305. }
  306. })
  307. };
  308. //完成订单
  309. User.prototype.finishOrder = function(orderID, callback) {
  310. var that = this;
  311. api.finishOrder({
  312. order_id: orderID,
  313. user_id: this.id
  314. }, function(res){
  315. if (typeof(callback) == 'function') {
  316. callback(res);
  317. }
  318. })
  319. };
  320. //取消订单
  321. User.prototype.cancelOrder = function (orderID, callback) {
  322. var that = this;
  323. api.cancelOrder({
  324. user_id: this.id,
  325. order_id: orderID
  326. }, function (res) {
  327. if (res.success) {
  328. // that.id = res.data.id;
  329. }
  330. if (typeof (callback) == 'function') {
  331. callback(res);
  332. }
  333. })
  334. };
  335. //获得技术列表
  336. //serviceType-服务类型
  337. //bookingTime-预订时间
  338. //addressID-地址
  339. User.prototype.getTechList = function (serviceType, bookingTime, addressID, callback) {
  340. var that = this;
  341. api.selectTech({
  342. service_type: serviceType,
  343. booking_time: bookingTime,
  344. address_id: addressID,
  345. user_id: that.id
  346. }, function (res) {
  347. if (res.success) {
  348. that.selectTech.can = res.data.can_select_tech;//可选择技术
  349. that.selectTech.match = res.data.service_match_tech;//服务匹配技术
  350. }
  351. if (typeof (callback) == 'function') {
  352. callback(res);
  353. }
  354. })
  355. };
  356. //登录数据
  357. function loginWithData(res){
  358. if (res.success) {
  359. var userData = res.data;
  360. if (userData.id) {
  361. this.id = userData.id;
  362. this.name = userData.user_name;
  363. this.mobile = userData.mobile;
  364. this.openId = userData.openid;
  365. this.info = userData;
  366. this.balance = userData.balance;
  367. this.wx_pub_openid = userData.wx_pub_openid;
  368. if ((config.isAndroid || config.isIOS) && this.lastID != this.id) {
  369. //换用户了,清掉地址信息
  370. order.set('address', null, true);
  371. }
  372. this.set('lastID', this.id);
  373. }
  374. }
  375. }
  376. return new User();//new User函数
  377. })