123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- /**
- * 用户模型
- */
- 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;
- }
- // localStorage.getItem('xyhwxUserID')
- User.instance = this;
- this.storagePrefix = 'user_';
- this.id = localStorage.getItem('xyhwxUserID'); //5870c935a84ea0417e8b456f
- this.lastID = this.getCache('lastID', null, '');
- this.name = '';
- this.avatar='';
- this.mobile = '';
- this.balance = 0;
- 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.orderCoupons = [];// 所有优惠券按顺序排好
- this.usable_coupons = [];// 可用优惠券
- this.used_coupons = [];// 用过的优惠券
- this.overtime_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();//新建base()函数.继承User函数的原型
- //获取用户信息
- User.prototype.getUserInfo = function (callback) {
- var that = this;
- console.log(that)
- native.getUserInfo(function (res) {
- loginWithData.call(that, res);//loginWithData-登录数据
- if (typeof(callback) == 'function') {//判断callback是否为函数.
- callback();//调用函数
- }
- });
- };
- // 获取用户的所有代金券
- User.prototype.getCouponList = function (callback) {
- var that = this;
- api.getCouponList({
- get_all: 1,
- user_id: that.id
- }, function (res) {
- if (res.success) {
- // 现有可用优惠券,按照面额降序排序;已经过期的优惠券,按照过期时间由近到远排序
- var now = new Date().getTime() / 1000;
- that.usable_coupons = res.data.useable_coupons;
- that.used_coupons = res.data.used_coupons;
- that.overtime_coupons = res.data.overtime_coupons;
- // that.usable_coupons.sort(function (a, b) {
- // return b.coupon.value - a.coupon.value;
- // });
- that.usable_coupons.sort(function (a, b) {
- return b.start_time-a.start_time ;
- });
- that.used_coupons.sort(function (a, b) {
- return a.end_time - b.end_time;
- });
- that.overtime_coupons.sort(function (a, b) {
- return a.end_time - b.end_time;
- });
- that.usable_coupons.forEach(function (item, index) {
- var isRemind = (item.end_time - now) / (60 * 60 * 24) <= 7;
- item.isRemind = isRemind;
- });
- that.orderCoupons = that.usable_coupons.concat(that.used_coupons).concat(that.overtime_coupons);
- }
- if (typeof(callback) == 'function') {
- callback(res);
- }
- });
- };
- //兑换优惠券 api预先定义的函数
- 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;
- console.log(that);
- this.getUserInfo(function (res) {//在获取用户信息
- if (that.id == '') {
- native.login(function (resA) {//本地中读取是否存在登陆数据
- loginWithData.call(that, resA);//调用登录数据
- if (typeof(callback) == 'function') {//判断callback是否为函数,true让其回调
- 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') {//用户ID是否为空并且callback是否为函数
- 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);//concat 2个数组
- } 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//订单ID为orderID
- }, function (res) {
- if (typeof(callback) == 'function') {
- callback(res);
- }
- });
- };
- //订单信息
- //遍历函数。将所有产生的订单信息赋值给orderInfo
- 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);
- }
- })
- };
- //获得技术列表
- //serviceType-服务类型
- //bookingTime-预订时间
- //addressID-地址
- 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.avatar = userData.avatar;
- this.openId = userData.openid;
- this.info = userData;
- this.balance = userData.balance;
- 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();//new User函数
- })
|