| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace app\api\controller;
- use app\api\logic\CommonLogic;
- use app\common\controller\Api;
- use app\api\logic\TeacLogin;
- use app\common\model\LedgerTeacAngelChangeModel;
- use app\common\model\LedgerTokenChangeModel;
- use app\common\model\LedgerWalletModel;
- use think\Log;
- use fast\Asset;
- use Exception;
- use app\common\model\UserModel;
- use think\Db;
- use app\common\model\LedgerTeacEcolyChangeModel;
- use app\common\library\Token;
- use think\Env;
- use app\common\model\ProductOrder;
- use function PHPSTORM_META\type;
- //茶宝Api
- class Chabao extends Api
- {
- protected array $noNeedLogin = ['*'];
- protected $params = [];
- public function _initialize()
- {
- parent::_initialize();
- $this->params = $this->request->post();
- if (empty($this->params['rwa_set'])) {
- if (empty($this->params) || !array_filter($this->params)) $this->error('参数错误');
- $sign = '';
- foreach ($this->params as $key => $value) {
- if ($key != 'sign') $sign .= $value;
- }
- $sign .= Env::get('rwa.AppID');
- $sign = md5($sign);
- if ($this->params['sign'] != $sign) $this->error('签名验签错误');
- }
- }
- /*
- * 判断登录
- */
- public function login(UserModel $userModel)
- {
- $user = $userModel::where('phone', $this->params['mobile'])->find();
- if (!$user) $this->error('账号不存在', [], 4000);
- Token::marshal($user['id'], $user['address']);
- $user['token'] = Token::getEncryptedToken($user['id']);
- $this->success('ok', $user);
- }
- /*
- * 注册
- */
- public function register(UserModel $userModel)
- {
- //手机号存在
- if ($userModel::where('phone', $this->params['mobile'])->find()) $this->error('账号已存在');
- $newUserID = 0;
- Db::startTrans();
- try {
- //注册
- $time = time();
- $data = [
- 'phone' => $this->params['mobile'],
- 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99), //188开头,时间戳去掉开头两位,再后面再加两位随机数
- 'avatar' => $this->request->domain() . '/assets/img/logo.png',
- 'create_time' => $time,
- 'team_level_id' => 0,
- ];
- // 创建用户
- $newUserID = $userModel->insertGetId($data);
- // 创建钱包
- (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
- Token::marshal($newUserID);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($newUserID == 0) $this->error('注册失败');
- $this->success('ok', ['token' => Token::getEncryptedToken($newUserID)]);
- }
- //绑定地址
- public function bindAddress(UserModel $userModel)
- {
- $user = $userModel->getByAddress($this->params['address']);
- if (!$user) $this->error('钱包地址不存在');
- if ($user->phone) $this->error('钱包地址已被绑定');
- //修改手机号
- $user->phone = $this->params['mobile'];
- $user->save();
- $this->success('ok');
- }
- //转让茶宝
- public function chalink(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
- {
- $user = $userModel->getByPhone($this->params['mobile']);
- if (!$user) $this->error('钱包地址不存在');
- //添加茶宝
- $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $this->params['chalink'], LedgerTokenChangeModel::ChaLink);
- $this->success('ok');
- }
- //转入Teac
- public function teac(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
- {
- $user = $userModel->getByPhone($this->params['mobile']);
- if (!$user) $this->error('钱包地址不存在');
- //添加Teac
- $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ANGEL, $this->params['teac'], LedgerTeacAngelChangeModel::TransferIn);
- $this->success('ok');
- }
- //转入TeaC生态发展
- public function teacecology(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
- {
- $user = $userModel->getByPhone($this->params['mobile']);
- if (!$user) $this->error('钱包地址不存在');
- //添加Teac
- $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ECOLY, $this->params['teacecology'], LedgerTeacEcolyChangeModel::TransferIn);
- $this->success('ok');
- }
- //茶付宝消费金额达到指定区间,赠送商品
- public function give_rwa_goods_list()
- {
- $userModel = new UserModel();
- $order_model = new ProductOrder();
- $data = $this->params['data'];
- $mobile_list = array_column($data, 'mobile');
- $user = $userModel->whereIn('phone', $mobile_list)->field('phone,id')->select();
- foreach ($data as &$item) {
- $item['gift_record_type'] = 2;
- $item['reason'] = '手机号未注册';
- foreach($user as &$item_1){
- if($item['mobile']==$item_1['phone']){
- $ret = $order_model->setGiverwagoods(0, 0, $item['product_id'], $item_1['id'], $order_model::Giveaway);
- if ($ret) {
- $item['gift_record_type'] = 1;
- $item['reason'] = '赠送RWA茶成功';
- } else {
- $item['gift_record_type'] = 0;
- $item['reason'] = '赠送RWA茶失败';
- }
- break;
- }
- }
- }
- unset($mobile_list);
- unset($user);
- $this->success('赠送RWA茶', $data);
- }
- }
|