<?php

namespace app\api\controller;
use think\facade\Lang;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 卖家登录控制器
 */
class Sellerlogin extends MobileMall {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/sellerlogin.lang.php');
    }

    /**
     * @api {POST} api/Sellerlogin/index 店铺登录
     * @apiVersion 1.0.0
     * @apiGroup Sellerlogin
     * 
     * @apiParam {String} client_type 客户端类型 android wap wechat ios windows jswechat
     * @apiParam {String} seller_name 卖家账号
     * @apiParam {String} password 登录密码
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {String} result.member_token  用户token
     * @apiSuccess {Object} result.member_info 用户信息
     * @apiSuccess {Int} result.member_info.member_id  用户ID
     * @apiSuccess {String} result.member_info.member_name  用户名称
     * @apiSuccess {String} result.member_info.member_truename  真实姓名
     * @apiSuccess {String} result.member_info.member_avatar  头像
     * @apiSuccess {Int} result.member_info.member_points  积分
     * @apiSuccess {String} result.member_info.member_email  邮箱
     * @apiSuccess {String} result.member_info.member_mobile  手机号
     * @apiSuccess {String} result.member_info.member_qq  QQ
     * @apiSuccess {String} result.member_info.member_ww  旺旺
     * @apiSuccess {String} result.token  卖家token
     * @apiSuccess {Object} result.info  卖家信息
     * @apiSuccess {Int} result.info.store_id  店铺ID
     * @apiSuccess {Int} result.info.member_id  用户ID
     * @apiSuccess {Int} result.info.seller_id  卖家ID
     * @apiSuccess {String} result.info.seller_name  卖家账号
     * @apiSuccess {String} result.info.store_avatar  店铺头像
     * @apiSuccess {Int} result.info.is_platform_store  是否自营店铺 0否1是
     * @apiSuccess {Int} result.info.storeclass_id  店铺分类ID
     */
    public function index() {
        $client=input('post.client_type');
        if (empty(input('post.seller_name')) || empty(input('post.password')) || !in_array($client, $this->client_type_array)) {
            ds_json_encode(10001, lang('param_error'));
        }
        $seller_model = model('seller');
        $seller_info = $seller_model->getSellerInfo(array('seller_name' => input('post.seller_name')));
        if (!$seller_info) {
            ds_json_encode(10001, lang('seller_account_not_exisit'));
        }
        //验证身份
        $member_model = model('member');
        $member_info = $member_model->getMemberInfo(array(
            'member_id' => $seller_info['member_id'],
            'member_password' => md5(input('post.password'))
        ));


        if (!$member_info) {
            ds_json_encode(10001, lang('password_error'));
        }

        //读取店铺信息
        $store_model = model('store');
        $store_info = $store_model->getStoreInfoByID($seller_info['store_id']);

        //更新卖家登陆时间
        $seller_model->editSeller(array('last_logintime' => TIMESTAMP), array('seller_id' => $seller_info['seller_id']));

        //生成登录令牌
        $token = self::_get_seller_token($seller_info['seller_id'], $seller_info['seller_name'], $client);
        if ($token) {
            $result = array();
            $result['token'] = $token;
            $result['info'] = $this->getSellerUser($seller_info,$store_info);
            $result['member_info'] = $this->getMemberUser($member_info);
            $token = $member_model->getBuyerToken($member_info['member_id'], $member_info['member_name'], $client);
            if(!$token){
                ds_json_encode(10001, lang('ds_common_op_fail'));
            }
            $result['member_token'] = $token;
            ds_json_encode(10000, '', $result);
        } else {
            ds_json_encode(10001, lang('ds_common_op_fail'));
        }
    }

    /**
     * 登录生成token
     */
    public static function _get_seller_token($seller_id, $seller_name, $client) {
        $mbsellertoken_model = model('mbsellertoken');

        //重新登录后以前的令牌失效
        $condition = array();
        $condition[] = array('seller_id','=',$seller_id);
        $condition[] = array('seller_clienttype','=',$client);
        $mbsellertoken_model->delMbsellertoken($condition);

        //生成新的token
        $mb_seller_token_info = array();
        $token = md5($seller_name . strval(TIMESTAMP) . strval(rand(0, 999999)));
        $mb_seller_token_info['seller_id'] = $seller_id;
        $mb_seller_token_info['seller_name'] = $seller_name;
        $mb_seller_token_info['seller_token'] = $token;
        $mb_seller_token_info['seller_logintime'] = TIMESTAMP;
        $mb_seller_token_info['seller_clienttype'] = $client;

        $result = $mbsellertoken_model->addMbsellertoken($mb_seller_token_info);

        if ($result) {
            return $token;
        } else {
            return null;
        }
    }

}
