user.js 8.5 KB

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