user.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.info = {};
  16. this.orders = {
  17. 1: {
  18. data: [],
  19. page: 0,
  20. hasMore: false
  21. },
  22. 2: {
  23. data: [],
  24. page: 0,
  25. hasMore: false
  26. },
  27. 3: {
  28. data: [],
  29. page: 0,
  30. hasMore: false
  31. },
  32. 4: {
  33. data: [],
  34. page: 0,
  35. hasMore: false
  36. },
  37. 5: {
  38. data: [],
  39. page: 0,
  40. hasMore: false
  41. }
  42. };
  43. this.appendOrder = {}
  44. this.coupons = [];
  45. this.orderCoupons = [];
  46. this.unuseable_coupons = [];
  47. this.activities = this.getCache('activities', null, {
  48. "visited_dog": false,
  49. "visited_order0104": false
  50. });
  51. this.productVisit = this.getCache('productVisit', null, {
  52. 1: {
  53. visited: false
  54. },
  55. 2: {
  56. visited: false
  57. },
  58. 3: {
  59. visited: false
  60. },
  61. 4: {
  62. visited: false
  63. },
  64. 5: {
  65. visited: false
  66. },
  67. 6: {
  68. visited: false
  69. },
  70. 7: {
  71. visited: false
  72. },
  73. 8: {
  74. visited: false
  75. },
  76. 9: {
  77. visited: false
  78. },
  79. 10: {
  80. visited: false
  81. },
  82. 11: {
  83. visited: false
  84. },
  85. 12: {
  86. visited: false
  87. },
  88. 13: {
  89. visited: false
  90. },
  91. 14: {
  92. visited: false
  93. },
  94. 15: {
  95. visited: false
  96. }
  97. });
  98. this.selectTech = {
  99. can: [],
  100. match: []
  101. };
  102. this.isCheck = this.getCache('isCheck', null, {
  103. "couponCheck": false
  104. });
  105. }
  106. User.prototype = new base();
  107. User.prototype.getUserInfo = function(callback){
  108. var that = this;
  109. native.getUserInfo(function(res) {
  110. loginWithData.call(that, res);
  111. if (typeof(callback) == 'function') {
  112. callback();
  113. }
  114. });
  115. };
  116. User.prototype.getCouponName = function (couponID) {
  117. var couponName = '';
  118. for (var i in this.orderCoupons) {
  119. var couponInfo = this.orderCoupons[i];
  120. if (couponInfo.id == couponID) {
  121. couponName = couponInfo['coupon']['name'];
  122. break;
  123. }
  124. }
  125. return couponName;
  126. };
  127. User.prototype.getCouponValue = function (couponID) {
  128. var couponValue = 0;
  129. for (var i in this.orderCoupons) {
  130. var couponInfo = this.orderCoupons[i];
  131. if (couponInfo.id == couponID) {
  132. couponValue = couponInfo['coupon']['value'];
  133. break;
  134. }
  135. }
  136. return couponValue;
  137. };
  138. User.prototype.getMaxAvailableCoupon = function () {
  139. var couponSelectVal = 0;
  140. var couponTmp = 0;
  141. $.each(this.orderCoupons, function(index, val) {
  142. if (val.coupon.value > couponSelectVal) {
  143. couponSelectVal = val.coupon.value;
  144. couponTmp = index;
  145. };
  146. });
  147. return {
  148. couponSelectVal: couponSelectVal,
  149. couponIndex: couponTmp
  150. }
  151. };
  152. User.prototype.getAvailableCoupon = function () {
  153. var couponSelectVal = 0;
  154. var couponTmp = 0;
  155. $.each(this.orderCoupons, function(index, val) {
  156. if (val.coupon.value > couponSelectVal) {
  157. couponSelectVal = val.coupon.value;
  158. couponTmp = index;
  159. };
  160. });
  161. return {
  162. couponSelectVal: couponSelectVal,
  163. couponIndex: couponTmp
  164. }
  165. };
  166. User.prototype.getCouponList = function(userId, callback){
  167. var that = this;
  168. api.getCouponList({
  169. get_all: 1,
  170. user_id: userId // 这...
  171. }, function(res) {
  172. if (res.success) {
  173. // 现有可用优惠券,按照面额降序排序;已经过期的优惠券,按照过期时间由近到远排序
  174. var now = new Date().getTime() / 1000;
  175. var useable_coupons = res.data.useable_coupons;
  176. var used_coupons = res.data.used_coupons;
  177. var overtime_coupons = res.data.overtime_coupons;
  178. useable_coupons.sort(function (a, b) {
  179. return b.coupon.value - a.coupon.value;
  180. })
  181. used_coupons.sort(function (a, b) {
  182. return a.end_time - b.end_time;
  183. })
  184. overtime_coupons.sort(function (a, b) {
  185. return a.end_time - b.end_time;
  186. })
  187. useable_coupons.forEach(function(item, index){
  188. var isRemind = (item.end_time - now) / (60 * 60 * 24) <= 7;
  189. item.isRemind = isRemind;
  190. })
  191. that.coupons = res.data;
  192. }
  193. if (typeof(callback) == 'function') {
  194. callback(res);
  195. }
  196. });
  197. };
  198. User.prototype.getProductCoupon = function(products, time, type, callback){
  199. var that = this;
  200. api.getCouponList({
  201. products: products,
  202. booking_time: time,
  203. user_id: this.id,
  204. type: type
  205. }, function(res) {
  206. if (res.success) {
  207. that.orderCoupons = res.data;
  208. that.unuseable_coupons = res.unuseable_coupons;
  209. }
  210. if (typeof(callback) == 'function') {
  211. callback(res);
  212. }
  213. });
  214. };
  215. User.prototype.exchangeCoupon = function(couponCode, callback){
  216. var that = this;
  217. api.exchangeCoupon({
  218. user_id: that.id,
  219. exchange_code: couponCode
  220. }, function(res) {
  221. if (typeof(callback) == 'function') {
  222. callback(res);
  223. }
  224. });
  225. };
  226. User.prototype.goLogin = function(callback){
  227. var that = this;
  228. this.getUserInfo(function(res) {
  229. if (that.id == '') {
  230. native.login(function(resA) {
  231. loginWithData.call(that, resA);
  232. if (typeof(callback) == 'function') {
  233. callback();
  234. }
  235. });
  236. } else {
  237. if (typeof(callback) == 'function') {
  238. callback();
  239. }
  240. }
  241. });
  242. };
  243. User.prototype.checkLogin = function(callback){
  244. var that = this;
  245. if (this.id == '') {
  246. this.goLogin(function() {
  247. if (that.id != '' && typeof(callback) == 'function') {
  248. callback();
  249. }
  250. });
  251. } else {
  252. if (typeof(callback) == 'function') {
  253. callback();
  254. }
  255. }
  256. };
  257. User.prototype.getOrderList = function(userId, type, callback, more){
  258. var that = this;
  259. var page = this.orders[type].page + 1;
  260. if (!more) {
  261. page = 1;
  262. }
  263. api.getMyOrderList({
  264. user_id: userId,
  265. type: type,
  266. page: page
  267. }, function(res) {
  268. if (res.current_page >= res.sum_page) {
  269. res.current_page = res.sum_page;
  270. that.orders[type].hasMore = false;
  271. } else {
  272. that.orders[type].hasMore = true;
  273. }
  274. that.orders[type].page = res.current_page;
  275. if (more) {
  276. that.orders[type].data = that.orders[type].data.concat(res.data);
  277. } else {
  278. that.orders[type].data = res.data;
  279. }
  280. if (typeof(callback) == 'function') {
  281. callback(res);
  282. }
  283. });
  284. };
  285. User.prototype.getOrderDetail = function(orderID, callback) {
  286. var that = this;
  287. api.getOrderDetail({
  288. user_id: that.id,
  289. order_id: orderID
  290. }, function(res) {
  291. if (typeof(callback) == 'function') {
  292. callback(res);
  293. }
  294. });
  295. }
  296. User.prototype.getOrderInfo = function(orderID){
  297. var that = this;
  298. var orderInfo = {};
  299. var orderList = this.orders[1].data;
  300. orderList.forEach(function(e, i) {
  301. if (e.id == orderID ) {
  302. orderInfo = e;
  303. }
  304. })
  305. return orderInfo;
  306. };
  307. User.prototype.refundOrder = function(orderID, callback) {
  308. var that = this;
  309. var from = '';
  310. if (config.isChubao) from = 'chubao';
  311. api.refundOrder({
  312. order_id: orderID,
  313. user_id: this.id,
  314. from: from
  315. }, function(res){
  316. if (typeof(callback) == 'function') {
  317. callback(res);
  318. }
  319. })
  320. }
  321. User.prototype.finishOrder = function(orderID, callback) {
  322. var that = this;
  323. api.finishOrder({
  324. order_id: orderID,
  325. user_id: this.id
  326. }, function(res){
  327. if (typeof(callback) == 'function') {
  328. callback(res);
  329. }
  330. })
  331. }
  332. User.prototype.cancelOrder = function (orderID, callback) {
  333. var that = this;
  334. api.cancelOrder({
  335. user_id: this.id,
  336. order_id: orderID
  337. }, function (res) {
  338. if (res.success) {
  339. // that.id = res.data.id;
  340. }
  341. if (typeof (callback) == 'function') {
  342. callback(res);
  343. }
  344. })
  345. };
  346. User.prototype.getTechList = function (serviceType, bookingTime, addressID, callback) {
  347. var that = this;
  348. api.selectTech({
  349. service_type: serviceType,
  350. booking_time: bookingTime,
  351. address_id: addressID,
  352. user_id: that.id
  353. }, function (res) {
  354. if (res.success) {
  355. that.selectTech.can = res.data.can_select_tech;
  356. that.selectTech.match = res.data.service_match_tech;
  357. }
  358. if (typeof (callback) == 'function') {
  359. callback(res);
  360. }
  361. })
  362. };
  363. function loginWithData(res){
  364. if (res.success) {
  365. var userData = res.data;
  366. if (userData.id) {
  367. this.id = userData.id;
  368. this.name = userData.user_name;
  369. this.mobile = userData.mobile;
  370. this.openId = userData.openid;
  371. this.info = userData;
  372. this.wx_pub_openid = userData.wx_pub_openid;
  373. if ((config.isAndroid || config.isIOS) && this.lastID != this.id) {
  374. //换用户了,清掉地址信息
  375. order.set('address', null, true);
  376. }
  377. this.set('lastID', this.id);
  378. }
  379. }
  380. }
  381. return new User();
  382. })