|
|
@ -0,0 +1,61 @@ |
|
|
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api; |
|
|
|
|
|
use App\Http\Controllers\Controller; |
|
|
|
|
|
use App\Models\Agent; |
|
|
|
|
|
use App\Models\User; |
|
|
|
|
|
use EasyWeChat\Factory; |
|
|
|
|
|
use EasyWeChat\Kernel\Exceptions\DecryptException; |
|
|
|
|
|
use Illuminate\Http\Request; |
|
|
|
|
|
use Illuminate\Support\Facades\Cache; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 用户 |
|
|
|
|
|
* Class UserController |
|
|
|
|
|
* @package App\Http\Controllers\Api |
|
|
|
|
|
*/ |
|
|
|
|
|
class UserController extends Controller |
|
|
|
|
|
{ |
|
|
|
|
|
public function profile(Request $request) |
|
|
|
|
|
{ |
|
|
|
|
|
$formData = $request->only(['iv', 'encryptedData']); |
|
|
|
|
|
$request->validate([ |
|
|
|
|
|
'iv' => 'required', |
|
|
|
|
|
'encryptedData' => 'required', |
|
|
|
|
|
], [ |
|
|
|
|
|
'*.required' => '参数缺失' |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
$agent = Agent::query()->find($this->agent_id); //代理商数据
|
|
|
|
|
|
$config = config('wechat.mini_program.default'); |
|
|
|
|
|
$config = array_merge($config, [ |
|
|
|
|
|
'app_id' => $agent->appid, |
|
|
|
|
|
'secret' => $agent->appsecret, |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
$session = Cache::get('session_key_' . $this->user_id); |
|
|
|
|
|
if (!$session) { |
|
|
|
|
|
return $this->error('无效的session,请重新登录'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$app = Factory::miniProgram($config); |
|
|
|
|
|
try { |
|
|
|
|
|
$decryptedData = $app->encryptor->decryptData($session, $formData['iv'], $formData['encryptedData']); |
|
|
|
|
|
$user = User::find($this->user_id); |
|
|
|
|
|
if (!empty($decryptedData['nickName'])) { |
|
|
|
|
|
$user->nickname = $decryptedData['nickName']; |
|
|
|
|
|
} |
|
|
|
|
|
if (!empty($decryptedData['avatarUrl'])) { |
|
|
|
|
|
$user->avatar = $decryptedData['avatarUrl']; |
|
|
|
|
|
} |
|
|
|
|
|
if (!empty($decryptedData['purePhoneNumber'])) { |
|
|
|
|
|
$user->mobile = $decryptedData['purePhoneNumber']; |
|
|
|
|
|
} |
|
|
|
|
|
$user->save(); |
|
|
|
|
|
} catch (\Exception | DecryptException $e) { |
|
|
|
|
|
return $this->error($e->getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $this->success($decryptedData); |
|
|
|
|
|
} |
|
|
|
|
|
} |