user.js 11 KB

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