user.js 8.4 KB

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