/** * 用户模型 */ define(['base', '$', 'native', 'api', 'order', 'config', 'address'], function(base, $, native, api, order, config, address) { var User = function() { if (typeof User.instance === 'object') { return User.instance; } User.instance = this; this.storagePrefix = 'user_'; this.id = ''; this.lastID = this.getCache('lastID', null, ''); this.name = ''; this.mobile = ''; this.info = {}; this.orders = { 1: { data: [], page: 0, hasMore: false }, 2: { data: [], page: 0, hasMore: false }, 3: { data: [], page: 0, hasMore: false }, 4: { data: [], page: 0, hasMore: false }, 5: { data: [], page: 0, hasMore: false } }; this.appendOrder = {} this.coupons = []; this.orderCoupons = []; this.unuseable_coupons = []; this.activities = this.getCache('activities', null, { "visited_dog": false, "visited_order0104": false }); this.productVisit = this.getCache('productVisit', null, { 1: { visited: false }, 2: { visited: false }, 3: { visited: false }, 4: { visited: false }, 5: { visited: false }, 6: { visited: false }, 7: { visited: false }, 8: { visited: false }, 9: { visited: false }, 10: { visited: false }, 11: { visited: false }, 12: { visited: false }, 13: { visited: false }, 14: { visited: false }, 15: { visited: false } }); this.selectTech = { can: [], match: [] }; this.isCheck = this.getCache('isCheck', null, { "couponCheck": false }); } User.prototype = new base(); User.prototype.getUserInfo = function(callback){ var that = this; native.getUserInfo(function(res) { loginWithData.call(that, res); if (typeof(callback) == 'function') { callback(); } }); }; User.prototype.getCouponName = function (couponID) { var couponName = ''; for (var i in this.orderCoupons) { var couponInfo = this.orderCoupons[i]; if (couponInfo.id == couponID) { couponName = couponInfo['coupon']['name']; break; } } return couponName; }; User.prototype.getCouponValue = function (couponID) { var couponValue = 0; for (var i in this.orderCoupons) { var couponInfo = this.orderCoupons[i]; if (couponInfo.id == couponID) { couponValue = couponInfo['coupon']['value']; break; } } return couponValue; }; User.prototype.getMaxAvailableCoupon = function () { var couponSelectVal = 0; var couponTmp = 0; $.each(this.orderCoupons, function(index, val) { if (val.coupon.value > couponSelectVal) { couponSelectVal = val.coupon.value; couponTmp = index; }; }); return { couponSelectVal: couponSelectVal, couponIndex: couponTmp } }; User.prototype.getAvailableCoupon = function () { var couponSelectVal = 0; var couponTmp = 0; $.each(this.orderCoupons, function(index, val) { if (val.coupon.value > couponSelectVal) { couponSelectVal = val.coupon.value; couponTmp = index; }; }); return { couponSelectVal: couponSelectVal, couponIndex: couponTmp } }; User.prototype.getCouponList = function(userId, callback){ var that = this; api.getCouponList({ get_all: 1, user_id: userId // 这... }, function(res) { if (res.success) { // 现有可用优惠券,按照面额降序排序;已经过期的优惠券,按照过期时间由近到远排序 var now = new Date().getTime() / 1000; var useable_coupons = res.data.useable_coupons; var used_coupons = res.data.used_coupons; var overtime_coupons = res.data.overtime_coupons; useable_coupons.sort(function (a, b) { return b.coupon.value - a.coupon.value; }) used_coupons.sort(function (a, b) { return a.end_time - b.end_time; }) overtime_coupons.sort(function (a, b) { return a.end_time - b.end_time; }) useable_coupons.forEach(function(item, index){ var isRemind = (item.end_time - now) / (60 * 60 * 24) <= 7; item.isRemind = isRemind; }) that.coupons = res.data; } if (typeof(callback) == 'function') { callback(res); } }); }; User.prototype.getProductCoupon = function(products, time, type, callback){ var that = this; api.getCouponList({ products: products, booking_time: time, user_id: this.id, type: type }, function(res) { if (res.success) { that.orderCoupons = res.data; that.unuseable_coupons = res.unuseable_coupons; } if (typeof(callback) == 'function') { callback(res); } }); }; User.prototype.exchangeCoupon = function(couponCode, callback){ var that = this; api.exchangeCoupon({ user_id: that.id, exchange_code: couponCode }, function(res) { if (typeof(callback) == 'function') { callback(res); } }); }; User.prototype.goLogin = function(callback){ var that = this; this.getUserInfo(function(res) { if (that.id == '') { native.login(function(resA) { loginWithData.call(that, resA); if (typeof(callback) == 'function') { callback(); } }); } else { if (typeof(callback) == 'function') { callback(); } } }); }; User.prototype.checkLogin = function(callback){ var that = this; if (this.id == '') { this.goLogin(function() { if (that.id != '' && typeof(callback) == 'function') { callback(); } }); } else { if (typeof(callback) == 'function') { callback(); } } }; User.prototype.getOrderList = function(userId, type, callback, more){ var that = this; var page = this.orders[type].page + 1; if (!more) { page = 1; } api.getMyOrderList({ user_id: userId, type: type, page: page }, function(res) { if (res.current_page >= res.sum_page) { res.current_page = res.sum_page; that.orders[type].hasMore = false; } else { that.orders[type].hasMore = true; } that.orders[type].page = res.current_page; if (more) { that.orders[type].data = that.orders[type].data.concat(res.data); } else { that.orders[type].data = res.data; } if (typeof(callback) == 'function') { callback(res); } }); }; User.prototype.getOrderDetail = function(orderID, callback) { var that = this; api.getOrderDetail({ user_id: that.id, order_id: orderID }, function(res) { if (typeof(callback) == 'function') { callback(res); } }); } User.prototype.getOrderInfo = function(orderID){ var that = this; var orderInfo = {}; var orderList = this.orders[1].data; orderList.forEach(function(e, i) { if (e.id == orderID ) { orderInfo = e; } }) return orderInfo; }; User.prototype.refundOrder = function(orderID, callback) { var that = this; var from = ''; if (config.isChubao) from = 'chubao'; api.refundOrder({ order_id: orderID, user_id: this.id, from: from }, function(res){ if (typeof(callback) == 'function') { callback(res); } }) } User.prototype.finishOrder = function(orderID, callback) { var that = this; api.finishOrder({ order_id: orderID, user_id: this.id }, function(res){ if (typeof(callback) == 'function') { callback(res); } }) } User.prototype.cancelOrder = function (orderID, callback) { var that = this; api.cancelOrder({ user_id: this.id, order_id: orderID }, function (res) { if (res.success) { // that.id = res.data.id; } if (typeof (callback) == 'function') { callback(res); } }) }; User.prototype.getTechList = function (serviceType, bookingTime, addressID, callback) { var that = this; api.selectTech({ service_type: serviceType, booking_time: bookingTime, address_id: addressID, user_id: that.id }, function (res) { if (res.success) { that.selectTech.can = res.data.can_select_tech; that.selectTech.match = res.data.service_match_tech; } if (typeof (callback) == 'function') { callback(res); } }) }; function loginWithData(res){ if (res.success) { var userData = res.data; if (userData.id) { this.id = userData.id; this.name = userData.user_name; this.mobile = userData.mobile; this.openId = userData.openid; this.info = userData; this.wx_pub_openid = userData.wx_pub_openid; if ((config.isAndroid || config.isIOS) && this.lastID != this.id) { //换用户了,清掉地址信息 order.set('address', null, true); } this.set('lastID', this.id); } } } return new User(); })