user.js 9.1 KB

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