user.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.used_coupons.sort(function (a, b) {
  137. return a.end_time - b.end_time;
  138. });
  139. that.overtime_coupons.sort(function (a, b) {
  140. return a.end_time - b.end_time;
  141. });
  142. that.usable_coupons.forEach(function (item, index) {
  143. var isRemind = (item.end_time - now) / (60 * 60 * 24) <= 7;
  144. item.isRemind = isRemind;
  145. });
  146. that.orderCoupons = that.usable_coupons.concat(that.used_coupons).concat(that.overtime_coupons);
  147. }
  148. if (typeof(callback) == 'function') {
  149. callback(res);
  150. }
  151. });
  152. };
  153. //兑换优惠券 api预先定义的函数
  154. User.prototype.exchangeCoupon = function (couponCode, callback) {
  155. var that = this;
  156. api.exchangeCoupon({
  157. user_id: that.id,
  158. exchange_code: couponCode//兑换码--为优惠券编码
  159. }, function (res) {
  160. if (typeof(callback) == 'function') {
  161. callback(res);//回调函数传回
  162. }
  163. });
  164. };
  165. //判断当前用户是否登录.没有则让其去登录
  166. User.prototype.goLogin = function (callback) {
  167. var that = this;
  168. this.getUserInfo(function (res) {//在获取用户信息
  169. if (that.id == '') {
  170. native.login(function (resA) {//本地中读取是否存在登陆数据
  171. loginWithData.call(that, resA);//调用登录数据
  172. if (typeof(callback) == 'function') {//判断callback是否为函数,true让其回调
  173. callback();
  174. }
  175. });
  176. } else {
  177. if (typeof(callback) == 'function') {
  178. callback();
  179. }
  180. }
  181. });
  182. };
  183. //是否检查登陆凭据是否存在
  184. User.prototype.checkLogin = function (callback) {
  185. var that = this;
  186. if (this.id == '') {
  187. this.goLogin(function () {
  188. if (that.id != '' && typeof(callback) == 'function') {//用户ID是否为空并且callback是否为函数
  189. callback();
  190. }
  191. });
  192. } else {
  193. if (typeof(callback) == 'function') {
  194. callback();
  195. }
  196. }
  197. };
  198. //获取订单列表
  199. User.prototype.getOrderList = function (userId, type, callback, more) {
  200. var that = this;
  201. var page = this.orders[type].page + 1;
  202. if (!more) {
  203. page = 1;
  204. }
  205. api.getMyOrderList({
  206. user_id: userId,
  207. type: type,
  208. page: page
  209. }, function (res) {
  210. if (res.current_page >= res.sum_page) {//如果当前页订单大于等于总页数
  211. res.current_page = res.sum_page;
  212. that.orders[type].hasMore = false;
  213. } else {
  214. that.orders[type].hasMore = true;
  215. }
  216. that.orders[type].page = res.current_page;//当前网页
  217. if (more) {
  218. that.orders[type].data = that.orders[type].data.concat(res.data);//concat 2个数组
  219. } else {
  220. that.orders[type].data = res.data;
  221. }
  222. if (typeof(callback) == 'function') {
  223. callback(res);
  224. }
  225. });
  226. };
  227. //订单明细
  228. User.prototype.getOrderDetail = function (orderID, callback) {
  229. var that = this;
  230. api.getOrderDetail({
  231. user_id: that.id,
  232. order_id: orderID//订单ID为orderID
  233. }, function (res) {
  234. if (typeof(callback) == 'function') {
  235. callback(res);
  236. }
  237. });
  238. };
  239. //订单信息
  240. //遍历函数。将所有产生的订单信息赋值给orderInfo
  241. User.prototype.getOrderInfo = function (orderID) {
  242. var that = this;
  243. var orderInfo = {};//数组
  244. var orderList = this.orders[1].data;
  245. orderList.forEach(function (e, i) {//遍历函数
  246. if (e.id == orderID) {
  247. orderInfo = e;
  248. }
  249. })
  250. return orderInfo;
  251. };
  252. //退款单
  253. User.prototype.refundOrder = function (orderID, callback) {
  254. var that = this;
  255. var from = '';
  256. if (config.isChubao) from = 'chubao';
  257. api.refundOrder({
  258. order_id: orderID,
  259. user_id: this.id,
  260. from: from
  261. }, function (res) {
  262. if (typeof(callback) == 'function') {
  263. callback(res);
  264. }
  265. })
  266. };
  267. //完成订单
  268. User.prototype.finishOrder = function (orderID, callback) {
  269. var that = this;
  270. api.finishOrder({
  271. order_id: orderID,
  272. user_id: this.id
  273. }, function (res) {
  274. if (typeof(callback) == 'function') {
  275. callback(res);
  276. }
  277. })
  278. };
  279. //取消订单
  280. User.prototype.cancelOrder = function (orderID, callback) {
  281. var that = this;
  282. api.cancelOrder({
  283. user_id: this.id,
  284. order_id: orderID
  285. }, function (res) {
  286. if (res.success) {
  287. // that.id = res.data.id;
  288. }
  289. if (typeof (callback) == 'function') {
  290. callback(res);
  291. }
  292. })
  293. };
  294. //获得技术列表
  295. //serviceType-服务类型
  296. //bookingTime-预订时间
  297. //addressID-地址
  298. User.prototype.getTechList = function (serviceType, bookingTime, addressID, callback) {
  299. var that = this;
  300. api.selectTech({
  301. service_type: serviceType,
  302. booking_time: bookingTime,
  303. address_id: addressID,
  304. user_id: that.id
  305. }, function (res) {
  306. if (res.success) {
  307. that.selectTech.can = res.data.can_select_tech;//可选择技术
  308. that.selectTech.match = res.data.service_match_tech;//服务匹配技术
  309. }
  310. if (typeof (callback) == 'function') {
  311. callback(res);
  312. }
  313. })
  314. };
  315. //登录数据
  316. function loginWithData(res) {
  317. if (res.success) {
  318. var userData = res.data;
  319. if (userData.id) {
  320. this.id = userData.id;
  321. this.name = userData.user_name;
  322. this.mobile = userData.mobile;
  323. this.avatar = userData.avatar;
  324. this.openId = userData.openid;
  325. this.info = userData;
  326. this.balance = userData.balance;
  327. this.wx_pub_openid = userData.wx_pub_openid;
  328. if ((config.isAndroid || config.isIOS) && this.lastID != this.id) {
  329. //换用户了,清掉地址信息
  330. order.set('address', null, true);
  331. }
  332. this.set('lastID', this.id);
  333. }
  334. }
  335. }
  336. return new User();//new User函数
  337. })